import Vue from "vue"
import index from './index.vue'
import router from '@/router/router'
import store from '@/vuex/store'
import axios from '@/router/axios'
import Cache from '@/util/cache'
import 'iview/dist/styles/iview.css'
import fullscreen from 'vue-fullscreen'
import '@/util/enhance'
import MintUI from 'mint-ui'
import { Toast } from 'mint-ui'
import 'mint-ui/lib/style.css'
import '@/assets/iconfont/iconfont.css'
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
import VueScroller from 'vue-scroller'
Vue.use(VueScroller)
Vue.use(preview)
Vue.use(fullscreen)
Vue.use(MintUI)

Vue.prototype.$success = function(msg = '操作成功！') {
  Toast({
    message: msg,
    iconClass: 'iconfont icon-chenggong',
    duration: '1000'
  })
}
Vue.prototype.$error = function(msg) {
  Toast({
    message: msg,
    iconClass: 'iconfont icon-icon_wrong',
    duration: '1000'
  })
}
Vue.prototype.$info = function(msg) {
  Toast({
    message: msg,
    duration: '4000'
  })
}

//全局过滤器
Vue.filter("filterPhoto",src=>{
  if( !src )
    return '/static/img/audit_details_01.png'
  else
    return src
})
Vue.filter("filterFontNumber",(content, length)=>{
  if(!content)
    return ''
  if(content.length<=length){
    return content
  }else{
    return content.substring(0,length)+'...';
  }
})
new Vue({
    router,
    axios,
    store,
    render: h => h(index)
}).$mount("#index");


//页面刷新时，重新赋值token
if (Cache.sessionGet('TOKEN')) {
  store.commit('RECORD_TOKEN', Cache.sessionGet('TOKEN'))
  store.commit('RECORD_LOGIN_USER', Cache.sessionGet('LOGIN_USER'))
}else {//token不存在
  console.log('去验证')
}

//每次跳转判断是否登录过
router.beforeEach((to, from, next) => {
  store.commit('UPDATE_SHOW_LOADING', true)
  store.commit('UPDAT_SHOW_CHECK_BOX', false)
  if (to.matched.some(r => r.meta.requireAuth)) {
    if (store.state.token) {
        next();
    }else {
      next({
          path: '/',
          query: {redirect: to.fullPath}
      })
    }
  }else {
    next();
  }
});
router.afterEach(route => {
  // store.commit('UPDATE_SHOW_LOADING', false)
});
